package br.com.gaacorp.sai.webapp.action;

import java.io.Serializable;

import br.com.gaacorp.sai.model.Answer;
import br.com.gaacorp.sai.webapp.action.BasePage;
import br.com.gaacorp.sai.service.GenericManager;

public class AnswerForm extends BasePage implements Serializable {
    private GenericManager<Answer, Long> answerManager;
    private Answer answer = new Answer();
    private Long answerId;

    public void setAnswerManager(GenericManager<Answer, Long> answerManager) {
        this.answerManager = answerManager;
    }

    public Answer getAnswer() {
        return answer;
    }

    public void setAnswer(Answer answer) {
        this.answer = answer;
    }

    public void setAnswerId(Long answerId) {
        this.answerId = answerId;
    }

    public String delete() {
        answerManager.remove(answer.getAnswerId());
        addMessage("answer.deleted");

        return "answerList";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (answerId != null && answerId != 0) {
            answer = answerManager.get(answerId);
        } else {
            answer = new Answer();
        }

        return "answerForm";
    }

    public String save() {
        boolean isNew = (answer.getAnswerId() == null || answer.getAnswerId() == 0);
        answerManager.save(answer);

        String key = (isNew) ? "answer.added" : "answer.updated";
        addMessage(key);

        if (isNew) {
            return "answerList";
        } else {
            return "answerForm";
        }
    }
} 